﻿using DevExpressWebApplication1.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DevExpressWebApplication1.Module.MM
{
    public partial class Stock : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gvStock_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            int sttId = int.Parse(hdfStock.Get("StockId").ToString());
            if (sttId > 0)
            {
                try
                {
                    StockUtils.Update(sttId, txtStock.Text, ControlUtils.GetCBBValue(cbbStockType));

                    gvStock.DataBind();
                }
                catch (Exception ex)
                {
                    Console.Write("Update stock type fails : " + ex.Message);
                }
            }
            else
            {
                try
                {
                    StockUtils.Insert(txtStock.Text, ControlUtils.GetCBBValue(cbbStockType));
                    gvStock.DataBind();
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }
        }

        protected void cbAddEdit_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            string[] split = e.Parameter.Split(';');
            if (split[0] == "Edit")
            {
                txtStock.Text = gvStock.GetRowValues(int.Parse(split[1].ToString()), "stock_name").ToString();
                ControlUtils.SetCBBValue(cbbStockType, gvStock.GetRowValues(int.Parse(split[1].ToString()), "stock_type_id").ToString()); 
                hdfStock.Set("StockId", gvStock.GetRowValues(int.Parse(split[1].ToString()), "stock_id").ToString());
            }
        }
    }
}